package com.nf.health.app.myfamily.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.BrokenLine;
import com.nf.health.app.customview.BrokenLineBackground;
import com.nf.health.app.models.DataDetailExtra;
import com.nf.health.app.models.DataDetails;
import com.nf.health.app.models.Family;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/** 
 * @ClassName: DataDetails2Activity 
 * @Description:详细数据
 * @author Ian 
 * @date 2015-11-4 上午9:46:33 
 *  
 */
public class DataDetails2Activity extends BaseActivity {

	private RadioGroup rg_details_titleId;
	private RadioButton rb_title;
	private List<DataDetails> devicelist = new ArrayList<DataDetails>();
	public static ArrayList<Float> yValue;
	private TextView tv_datadetails_device, tv_datadetails_history, tv_unit;
	private RelativeLayout rl_broken_line_bg;
	private LinearLayout rl_broken_line;
	private ArrayList<String> xValues;
	private ArrayList<String> yValues;
	private String currentTitle;
	private String currentUnit;
	private TextView tv_datasdetails_fenziId;
	private String deviceType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String deviceName = getIntent().getStringExtra("deviceName");
		
		
		
		deviceType = getIntent().getStringExtra("deviceType");
		
		Family family = (Family) getIntent().getSerializableExtra("member");
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_datadetails2);
		TextView titleTV = (TextView) findViewById(R.id.titlebar_title_tv);


		rg_details_titleId = (RadioGroup) findViewById(R.id.rg_details_titleId);
		tv_datadetails_device = (TextView) findViewById(R.id.tv_datadetails_deviceId);
		tv_datadetails_history = (TextView) findViewById(R.id.tv_datadetails_historyId);
		tv_unit = (TextView) findViewById(R.id.tv_unit);
		rl_broken_line_bg = (RelativeLayout) findViewById(R.id.rl_broken_line_bg);
		rl_broken_line = (LinearLayout) findViewById(R.id.rl_broken_line);

		tv_datasdetails_fenziId = ViewUtil.getView(this, R.id.tv_datasdetails_fenziId);

		//		yValues = new ArrayList<String>();
		//		xValues = new ArrayList<String>();
		yValue = new ArrayList<Float>();

		initListener();

		if(family != null){
			titleTV.setText(deviceName);
//			View rl_1 = ViewUtil.getView(this, R.id.rl_1);
//			rl_1.setVisibility(View.GONE);
			int pageNo = 1;
			int pageSize = 10;
//			provider.requestGetFamilyMemberItemData(family.getBoxid(), family.getBoxno(), family.getUserno(), family.getType(), family.getUserid(), deviceType, "", "", String.valueOf(pageNo), String.valueOf(pageSize), "itemData");
			provider.requestGetFamilyMemberItemData(family.getTouserid(), deviceType, "list", String.valueOf(pageNo), String.valueOf(pageSize), "itemData");

		}else{
			titleTV.setText(deviceName);
			tv_datadetails_device.setText(deviceName);
			provider.requestDataDetails(deviceType,"requestDataDetails");
		}

	}

	private void initListener() {
		tv_datadetails_history.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("xValues", xValues);
				bundle.putStringArrayList("yValues", yValues);
				bundle.putString("title", currentTitle);
				bundle.putString("unit", currentUnit);
				ActivityUtils.showActivity(DataDetails2Activity.this, DataDetailsHistoryActivity.class, bundle);
			}
		});
	}


	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		DataDetailExtra extra = (DataDetailExtra) object;
		//单独对血糖数据处理
//		if(DeviceData.Type.BLOOD_SUGAR.equals(deviceType)){
//			List<DataDetails> afterdeviceData = extra.getAfterdeviceData();
//			List<DataDetails> deviceData = extra.getDeviceData();
//			DataDetails details = null;
//			for(int i= 0;i<afterdeviceData.size();i++){
//				if("after_glucose".equals(afterdeviceData.get(i).getColumn_key())){
//					details = afterdeviceData.get(i);
//					break;
//				}
//			}
//			for(int i= 0;i<deviceData.size();i++){
//				if("after_glucose".equals(afterdeviceData.get(i).getColumn_key())){
//					if(details != null){
//						deviceData.set(i, details);
//					}
//				}
//			}
//		}
		List<DataDetails> tempList = extra.getDeviceData();
		tv_datasdetails_fenziId.setText("提示："+extra.getDeviceDesc());
		if (tempList != null) {
			devicelist.clear();
			devicelist.addAll(tempList);

			for (int i = 0; i < devicelist.size(); i++) {
				rb_title = new RadioButton(DataDetails2Activity.this);
				rb_title.setText(devicelist.get(i).getName());
				rb_title.setTextSize(16);

				//选中  白色  ，默认 黑色
				rb_title.setTextColor(getResources().getColorStateList(R.color.blackw_color_day));	

				rb_title.setBackgroundResource(R.drawable.rb_category_tab_title);
				rb_title.setButtonDrawable(android.R.color.transparent);
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.MATCH_PARENT);
				params.setMargins(8, 4, 8, 4);
				rb_title.setLayoutParams(params);
				rb_title.setPadding(16, 8, 16, 8);
				rb_title.setGravity(Gravity.CENTER);
				rb_title.setTag(i);
				rg_details_titleId.addView(rb_title);
				if (i == 0) {
					try {
						refreshView(0);
					} catch (Exception e) {
						e.printStackTrace();
					}
					rb_title.setChecked(true);
				}
			}
			rg_details_titleId
			.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup group,
						int checkedId) {
					int count = rg_details_titleId.getChildCount();
					for (int i = 0; i < count; i++) {
						RadioButton rb = (RadioButton) rg_details_titleId
								.getChildAt(i);
						if (rb.isChecked()) {
							try {
								refreshView(i);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						}
					}
				}
			});
		}
	}

	public void refreshView(int position) {
		currentTitle = devicelist.get(position).getName();
		currentUnit = devicelist.get(position).getUnit();

		tv_unit.setText("单位:  " + devicelist.get(position).getUnit());
		yValue.clear();
		xValues = devicelist.get(position).getxValue();
		yValues = devicelist.get(position).getyValue();
		ArrayList<String> y_array = devicelist.get(position).getY_array();
//				String max = devicelist.get(position).getMax();
//				String min = devicelist.get(position).getMin();
		String max = y_array.get(y_array.size()-1);
		String min = y_array.get(0);

		for (int i = 0; i < yValues.size(); i++) {
			if(!TextUtils.isEmpty(yValues.get(i))){
				try {
					yValue.add(Float.parseFloat(yValues.get(i)));
				} catch (Exception e) {
					if("-".equals(yValues.get(i))){
						yValue.add(Float.valueOf(0));
					}else if("-+".equals(yValues.get(i))){
						yValue.add(Float.valueOf(1));
					}else if("+".equals(yValues.get(i))){
						yValue.add(Float.valueOf(2));
					}else if("++".equals(yValues.get(i))){
						yValue.add(Float.valueOf(3));
					}else if("+++".equals(yValues.get(i))){
						yValue.add(Float.valueOf(4));
					}else{
						//存在<=0.2EU/dL
						yValue.add(Float.valueOf("0.0"));
					}
					if(yValues.get(i).contains("-")||yValues.get(i).contains("+")){
						max = "4";
						min = "0";
					}
				}
			}else{
				yValue.add(Float.valueOf(0));
			}
		}
		rl_broken_line_bg.removeAllViews();
		rl_broken_line_bg.addView(new BrokenLineBackground(this, y_array));
		rl_broken_line.removeAllViews();
		rl_broken_line.addView(new BrokenLine(this, yValue, Float.parseFloat(max),Float.parseFloat(min), y_array.size()));
	}
}
